package cn.adolf.carmaster.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;


public class InfoDialog extends DialogFragment {

    private String title;
    private String info;

    public InfoDialog(String title, String info, BtnListener btnListener) {
        this.title = title;
        this.info = info;
        mBtnListener = btnListener;
    }

    public interface BtnListener{
        void onClick(boolean isConfirm);
    }

    private BtnListener mBtnListener;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setMessage(info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mBtnListener!=null) {
                            mBtnListener.onClick(true);
                        }
                        dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (mBtnListener!=null) {
                            mBtnListener.onClick(false);
                        }
                        dismiss();
                    }
                }).create();
        return dialog;

    }
}
